package com.chrudgames.juicedietrecipes;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.SimpleCursorAdapter.ViewBinder;

public class Search extends ListActivity {
	
	 SimpleCursorAdapter adapter;
	 Cursor c;
	
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu){
		 MenuInflater inflater = getMenuInflater();
		 inflater.inflate(R.menu.options, menu);
		 return true;
	 }
	 
	 @Override
	    public void onResume()
	    {
		 FillScreen();
		 
		    
  	   super.onResume();
		 
	    }
	 
	 @Override
	 public boolean onOptionsItemSelected(MenuItem item){
		 switch (item.getItemId()){
		 
        case R.id.search:
       	 Intent i3 = new Intent(this, Search.class);
       	 	startActivity(i3);
       	 return true;
		 
		 case R.id.generald:
	    		Intent i = new Intent(this, General.class);
	          	startActivity(i);
			 return true;
			 
        case R.id.help:
        	Intent i2 = new Intent(this, Help.class);
           	startActivity(i2);
            return true;
            
			 default:
				 return super.onOptionsItemSelected(item);
		 }
	 }
	 

	/** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
    }
    
	public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.btnGo:
    	
    		FillScreen();
    		
          	break;
    	}
	}
    
    
	 protected void onListItemClick(ListView l, View v, int position, long id) {

  	   //super.onListItemClick(l, v, position, id);
         //Object o = this.getListAdapter().getItem(position);
  	   
         
         Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
         c.moveToPosition(position);
        
         final String lookupKey = c.getString(0);
     	
         c.close();
        
         Intent i = new Intent(this, details.class);
         Bundle b1 = new Bundle();
         

         b1.putString("lookupKey", lookupKey);

                 i.putExtras(b1);
                         
                 
               	startActivity(i);
      }
	
   private void FillScreen() {
   	
   	DatabaseHandler db = new DatabaseHandler(this);

      // db.reCreateDB();

              
        String[] fields = new String[]  {"name","ing","favorite","favorite"};
        int[] views = new int[] {R.id.text1,R.id.text2,R.id.favon,R.id.favoff};
        
        final TextView mTextView = (TextView) findViewById(R.id.txtSearch);
        
        String strSearch = mTextView.getText().toString() + "";
        
        if(strSearch!="") {
        
        c = (Cursor) db.getSearchRecipesCursor(strSearch);
       
       
       adapter = new SimpleCursorAdapter(
                this,
                R.layout.custom_row_view,
                c, fields, views);
        
        adapter.setViewBinder(new ProductViewBinder());

        setListAdapter(adapter);
       
       // adapter = null;
        
        //c.close();
        
        db.close();
   	
   	
        }   	
   }

   
   @Override
   public void onStop()
   {
	   
   	 adapter=null; 
   	 
   	   super.onStop();
   }
   
   private static class ProductViewBinder implements ViewBinder {

       public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
       	
       	
       	if (view.getId()==R.id.favoff) {
       		
       		
       		if(cursor.getString(6).contentEquals("0")){
       			
	        		        			
       			view.setVisibility(View.VISIBLE);
       			
       		}else{
       			
       			view.setVisibility(View.GONE);
       		}
       		
       		return true;
       	
       	
       		
       	}else if (view.getId()==R.id.favon) {
       		
       		
       		if(cursor.getString(6).contentEquals("1")){
       			
       			view.setVisibility(View.VISIBLE);
       			
       		}else{
       			
       			view.setVisibility(View.GONE);
       		}
       		
       		return true;
           }

          return false;
       }
   }

   
	
}
